/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sbbs.nettydisruptor.timeserver;

import org.sbbs.nettydisruptor.base.Executor;
import org.sbbs.nettydisruptor.base.Session;

/**
 *
 * @author MACHENIKE
 */
public class TimeServerExecutor implements Executor {

    protected Session session;
    protected Object request;

    public TimeServerExecutor(Session session, Object request) {
        this.session = session;
        this.request = request;
    }

    @Override
    public void onExecute() {
//        String body = (String) msg;
//	System.out.println("The time server receive order : " + body
//		+ " ; the counter is : " + ++counter);
//	String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new java.util.Date(
//		System.currentTimeMillis()).toString() : "BAD ORDER";
//	currentTime = currentTime + System.getProperty("line.separator");
//	ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
//	ctx.writeAndFlush(resp);
        String body = (String) request;
        String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new java.util.Date(
		System.currentTimeMillis()).toString() : "BAD ORDER";
        if (null == request) {
            throw new NullPointerException("request");
        }
        // TODO: Just send message back, do some logic on real
        System.out.println("currentTime is : " + currentTime);
        session.writeAndFlush(String.valueOf(currentTime));
    }

    @Override
    public void release() {
        this.session = null;
        this.request = null;
    }
}
